﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TimbreRecognition.Recognition.Model;

namespace TimbreRecognition.Recognition
{
    public class ResultTester
    {
        public double GetErrorNumber(Dictionary<double[], double[]> data, INetwork network)
        {
           INetwork classifier = new NetworkClassifierWrapper(network);

            int errorCount = 0;
            foreach (KeyValuePair<double[], double[]> entry in data)
            {
                double[] input = entry.Key;
                classifier.recalculateOutput(input);
                double[] actualResult = classifier.getOutput();

                double[] expectedOutput = entry.Value;

                if (actualResult.Length != expectedOutput.Length)
                {
                    throw new ArgumentException("Expected and actual arrays should have equal length");
                }

                for (int i = 0; i < expectedOutput.Length; i++)
                {
                    if (expectedOutput[i] != actualResult[i])
                    {
                        errorCount++;
                        break;
                    }
                }
            }

            return (double)errorCount / data.Count;
        }

        public List<CorrespondenceItem> GetExpectedToActualCorrespondenceMap(Dictionary<double[], double[]> data,
            INetwork network)
        {
            INetwork classifier = new NetworkClassifierWrapper(network);

            List<CorrespondenceItem> correspondence = new List<CorrespondenceItem>();
            foreach (KeyValuePair<double[], double[]> entry in data)
            {
                double[] input = entry.Key;
                classifier.recalculateOutput(input);
                double[] actualResult = classifier.getOutput();

                double[] expectedOutput = entry.Value;

                correspondence.Add(new CorrespondenceItem() {Expected = expectedOutput, Actual =  actualResult });
            }

            return correspondence;
        }
    }

    public class CorrespondenceItem
    {
        public double[] Expected { get; set; }

        public double[] Actual { get; set; }

    }
}
